import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import Users from './components/user/Users.vue'
import roles from './components/power/roles.vue'
import rights from './components/power/rights.vue'
import goods from './components/goods/goods.vue'
import params from './components/goods/params.vue'
import categories from './components/goods/categories.vue'
import orders from './components/orders/orders.vue'
import reports from './components/reports/reports.vue'


Vue.use(Router)

// 路由导航守卫: 有些页面登录才可以看 
const router = new Router({
  routes: [
    {
      path: '/login',
      // name: 'Login',
      component: Login
    },
    {
      path: '/', redirect: "/login"
    },
    {
      path: "/Home", component: Home,
      children:[
        {path: "/Welcome", component:Welcome},
        {path: "/Users", component:Users},
        {path:"/roles",component:roles},
        {path:"/rights",component:rights},
        {path:"/goods",component:goods},
        {path:"/params",component:params},
        {path:"/categories",component:categories},
        {path:"/orders",component:orders},
        {path:"/reports",component:reports}
      ]
    },
  
    

    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    // }
  ]
})


router.beforeEach((to, form, next) => {
  if (to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  if(!tokenStr) return next("/login")
  next()
})

export default router
